package com.kischang.blog.web.actionuser;

import com.kischang.blog.model.User;
import com.kischang.blog.service.UserService;
import com.kischang.blog.utils.CookieUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Scope("prototype")
public class LoginAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{

	private static final long serialVersionUID = -3671274626188709864L;
	
	public static final String SESSION_USER = "user";
	@Resource
	private UserService userService;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private String username;
	private String password;
	private String msg;
	private String url;
	private boolean autologin;
	private int stat;

	@Override
	public String execute() throws Exception {
		User user = userService.findUserByUsername(username);
		msg = "登陆成功！！！";
		stat = 0;
		// 判断用户名密码
		if (user == null || !user.getPassword().equals(password)) {
			msg = "用户名或密码错误！！！";
			stat = 1;
			return SUCCESS;
		}
		
		// 判断ticket
		String pwcookie = CookieUtils.getCookieValue(request);
		if(pwcookie == null){
			if(autologin)
				userService.updateTicketAndLastlogin(user,pwcookie);
			else
				userService.updateLastLoginTime(user);
			response.addCookie(CookieUtils.createCookie(user, 7));
		}else{
			pwcookie = pwcookie.substring(pwcookie.indexOf("|")+1);
			if(!userService.checkTicket(user,pwcookie)){
				if(autologin)
					userService.updateTicketAndLastlogin(user,pwcookie);
				else
					userService.updateLastLoginTime(user);
			}
		}
		//存入session
		ActionContext.getContext().getSession().put(SESSION_USER, user);
		return SUCCESS;
	}

	public String bycookie() throws Exception{
		Cookie cookie = CookieUtils.getCookie(request, CookieUtils.USER_COOKIE);
		if(cookie == null){
			msg = "自动登陆失败！！！";
			setStat(1);
			return SUCCESS;
		}
		username = cookie.getValue().substring(0, cookie.getValue().indexOf("|"));
		String ckpw = cookie.getValue().substring(cookie.getValue().indexOf("|")+1);
		User user = userService.findUserByUsername(username);
		msg = "登陆成功！！！";
		stat = 0;
		// 判断用户名
		if (user == null ) {
			msg = "用户名错误！！！";
			setStat(1);
			return SUCCESS;
		}
		//检测验证码
		if(!ckpw.equals(user.getTicket())){
			msg = "校验码错误！！！";
			setStat(1);
			return SUCCESS;
		}
		userService.updateLastLoginTime(user);
		//存入session
		ActionContext.getContext().getSession().put(SESSION_USER, user);
		msg = user.getNickname();
		return SUCCESS;
	}
	public String view(){
		return "view";
	}
	public String getMsg() {
		return msg;
	}
	public int getStat() {
		return stat;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setAutologin(boolean autologin) {
		this.autologin = autologin;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setStat(int stat) {
		this.stat = stat;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
}
